package com.photoeditor.demo.ui.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v4.widget.CircularProgressDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.util.FontUtil;

import static com.photoeditor.demo.util.measureTextViewSizeUtils.measureTextViewSize;

public class DownloadApplyButton extends RelativeLayout {

    private View mBgView;
    private TextView mApplyTextView;
    private ImageView mDownloadImgView;

    private int mProgress;
    private CircularProgressDrawable mProgressDrawable;
    private ValueAnimator mValueAnimator;

    public DownloadApplyButton(Context context) {
        this(context, null);
    }

    public DownloadApplyButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DownloadApplyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        findViews();
    }

    /**
     * 初始化view
     */
    private void findViews() {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_download_apply_btn, this);
        mApplyTextView = (TextView) findViewById(R.id.tv_apply);
        measureTextViewSize(getContext(),mApplyTextView,34);
        mDownloadImgView = (ImageView) findViewById(R.id.img_download);
        mBgView = (View) findViewById(R.id.view_bg);

        mProgressDrawable = new CircularProgressDrawable(getContext());
        mProgressDrawable.setStrokeWidth(DeviceUtils.dip2px(2));
        mProgressDrawable.setColorSchemeColors(getResources().getColor(R.color.download_btn_bg));

        FontUtil.setAvenirFontHeavy(mApplyTextView);
    }

    public void setProgress(int progress) {
        if (mProgress == 0) {
            mDownloadImgView.setImageDrawable(mProgressDrawable);
            mProgressDrawable.start();
            mBgView.setBackgroundResource(R.drawable.bg_apply_btn);
            mBgView.setVisibility(View.GONE);
        }
        mProgress = progress;
        if (mProgress == 100) {
            mProgressDrawable.stop();
            mDownloadImgView.setVisibility(View.GONE);
            mBgView.setVisibility(View.VISIBLE);

            mValueAnimator = ValueAnimator.ofInt(DeviceUtils.dip2px(20), DeviceUtils.dip2px(36));
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    int width = (int) valueAnimator.getAnimatedValue();
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mBgView.getLayoutParams();
                    layoutParams.width = width;
                    mBgView.setLayoutParams(layoutParams);
                }

            });
            mValueAnimator.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    mApplyTextView.setAlpha(1f);
                }

            });
            mValueAnimator.setDuration(500);
            mValueAnimator.start();
        }
    }

    public void setStatus(boolean installed) {
        mBgView.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mBgView.getLayoutParams();
        if (installed) {
            layoutParams.width = DeviceUtils.dip2px(36);
            mApplyTextView.setAlpha(1f);
            mDownloadImgView.setVisibility(View.GONE);
            mBgView.setBackgroundResource(R.drawable.bg_apply_btn);
        } else {
            layoutParams.width = DeviceUtils.dip2px(20);
            mApplyTextView.setAlpha(0f);
            mDownloadImgView.setImageResource(R.drawable.ic_home_ic_download);
            mDownloadImgView.setVisibility(View.VISIBLE);
            mBgView.setBackgroundResource(R.drawable.bg_circle_btn);
        }
        mBgView.setLayoutParams(layoutParams);
    }

    public void setDownloadFailedStatus() {
        mProgress = 0;
        setStatus(false);
    }

    public boolean isDownloading() {
        return mProgress != 0 && mProgress != 100;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mValueAnimator != null) {
            mValueAnimator.cancel();
            mValueAnimator.removeAllListeners();
            mValueAnimator = null;
        }
    }

}
